/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Day10_Ex2_collectionDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

/**
 *
 * @author Mark
 */
public class CollectionsDemo
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {

        //Using ArrayList
        List<String> strings = new ArrayList<String>();
        strings.add("hi");
        strings.add("there");
        String s = strings.get(0); // no cast required! 
        System.out.println(s);


        //Using Set
        Set<String> set = new HashSet<String>(); // instantiate a concrete set 
        // ...
        set.add("Mark"); // insert an elements 
        set.add("Mark"); // insert an elements 
        set.add("Mark"); // insert an elements 
        set.add("Fred"); // insert an elements 
        set.add("Mark"); // insert an elements 

        int n = set.size(); // get size 
        System.out.println("My size is:" + n);

        if (set.contains("Mark"))
        {
            System.out.println("YEP");// check membership 
        }

        // iterate through the set 
        Iterator<String> iter = set.iterator();
        while (iter.hasNext())
        {
            System.out.println(iter.next());

        }

        //Using Map
        Map<String, Integer> map = new HashMap<String, Integer>(); // instantiate a concrete map  
        map.put("M1", 4); // insert a key-value pair  
        map.put("M2", 6);
        map.put("M3", 5);

        // get the value associated with key 
        Object val = map.get("M2");
        map.remove("M1"); // remove a key-value pair
 
        if (map.containsValue(val))
        {
            System.out.println("yep - it's contains it");
        }

        Set<String> keys = map.keySet(); // get the set of keys

        // iterate through the set of keys 
        iter = keys.iterator();
        while (iter.hasNext())
        {
            System.out.println((String) iter.next());

        }


        //Demo Simple Collection (to be built up)

        //Example Progresses through Collection, AbstractCollection, ArrayList, Vector, Stack

        System.out.println("Simple Collection Demo");

        //Collection<String> myCollectionString = new ArrayList<String>();

        //AbstractCollection<String> myCollectionString = new ArrayList<String>();

        //List<String> myCollectionString = new ArrayList<String>();

        Vector<String> myCollectionString = new Vector<String>();

        myCollectionString.add("Mark");
        myCollectionString.add("Benny");
        myCollectionString.add("Sandra");

        //Simple Stack example
        //Stack<String> myCollectionStringStack = new Stack<String>();
        //myCollectionStringStack.push("Stack item 1");
        //myCollectionStringStack.push("Stack item 2");

        //Iterator of a stack breaks the rules of the stack!
        //for (Iterator<String> it = myCollectionStringStack.iterator(); it.hasNext();)
        //{
        //    System.out.println(it.next());
        //}


        System.out.println(myCollectionString.contains("Mark"));

        System.out.println(myCollectionString.size());

        //Traditional Iterator Loop
        Iterator<?> myIt = myCollectionString.iterator();
        while (myIt.hasNext())
        {
            System.out.println(myIt.next().toString());
        }


        //Set is an optional parameter of ArrayList, Vector only
        myCollectionString.set(0, "Marko");

        //For next iterator Loop
        for (Iterator<String> it = myCollectionString.iterator(); it.hasNext();)
        {
            System.out.println(it.next());
        }

        //To illustrate the utility of Collections
        //Collections.reverse(myCollectionString);

        //For next iterator Loop
        for (Iterator<String> it = myCollectionString.iterator(); it.hasNext();)
        {
            System.out.println(it.next());
        }

        //To illustrate the utility of Collections
        //Collections.sort(myCollectionString);

        //For next iterator Loop
        for (Iterator<String> it = myCollectionString.iterator(); it.hasNext();)
        {
            System.out.println(it.next());
        }
    }
}
